import { GameError } from './GameError';

/**
 * 系统相关错误类
 */

/**
 * 服务不可用错误
 */
export class ServiceUnavailableError extends GameError {
    readonly code = 'SERVICE_UNAVAILABLE';
    readonly statusCode = 503;

    constructor(serviceName: string) {
        super(`服务不可用：${serviceName}`, { serviceName });
    }
}

/**
 * 配置错误
 */
export class ConfigurationError extends GameError {
    readonly code = 'CONFIGURATION_ERROR';
    readonly statusCode = 500;

    constructor(configKey: string, reason: string) {
        super(`配置错误：${configKey}，原因：${reason}`, {
            configKey,
            reason
        });
    }
}

/**
 * 数据库错误
 */
export class DatabaseError extends GameError {
    readonly code = 'DATABASE_ERROR';
    readonly statusCode = 500;

    constructor(operation: string, reason: string) {
        super(`数据库操作失败：${operation}，原因：${reason}`, {
            operation,
            reason
        });
    }
}

/**
 * 网络错误
 */
export class NetworkError extends GameError {
    readonly code = 'NETWORK_ERROR';
    readonly statusCode = 500;

    constructor(operation: string, reason: string) {
        super(`网络操作失败：${operation}，原因：${reason}`, {
            operation,
            reason
        });
    }
}

/**
 * 认证错误
 */
export class AuthenticationError extends GameError {
    readonly code = 'AUTHENTICATION_ERROR';
    readonly statusCode = 401;

    constructor(reason: string = '身份验证失败') {
        super(reason);
    }
}

/**
 * 授权错误
 */
export class AuthorizationError extends GameError {
    readonly code = 'AUTHORIZATION_ERROR';
    readonly statusCode = 403;

    constructor(action: string, reason: string = '权限不足') {
        super(`操作 ${action} 被拒绝：${reason}`, {
            action,
            reason
        });
    }
}

/**
 * 参数验证错误
 */
export class ValidationError extends GameError {
    readonly code = 'VALIDATION_ERROR';
    readonly statusCode = 400;

    constructor(field: string, value: any, constraint: string) {
        super(`参数验证失败：${field}=${value}，约束：${constraint}`, {
            field,
            value,
            constraint
        });
    }
}

/**
 * 业务逻辑错误
 */
export class BusinessLogicError extends GameError {
    readonly code = 'BUSINESS_LOGIC_ERROR';
    readonly statusCode = 400;

    constructor(operation: string, reason: string) {
        super(`业务逻辑错误：${operation}，原因：${reason}`, {
            operation,
            reason
        });
    }
}

/**
 * 资源不足错误
 */
export class InsufficientResourceError extends GameError {
    readonly code = 'INSUFFICIENT_RESOURCE';
    readonly statusCode = 400;

    constructor(resourceType: string, required: number, available: number) {
        super(`${resourceType}不足，需要：${required}，可用：${available}`, {
            resourceType,
            required,
            available
        });
    }
}

/**
 * 操作超时错误
 */
export class OperationTimeoutError extends GameError {
    readonly code = 'OPERATION_TIMEOUT';
    readonly statusCode = 408;

    constructor(operation: string, timeout: number) {
        super(`操作超时：${operation}，超时时间：${timeout}ms`, {
            operation,
            timeout
        });
    }
}
